# encoding: utf-8
from flask_marshmallow import base_fields
from flask_restplus_patched import ModelSchema
from app.modules.common.schemas import BaseSchema
from app.modules.project.schemas import ProjectBaseSchema
from .models import ProjectPlanInfo, PlanMember


class TrunkVcsUrlBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'url',  # 分支url
            'name',  # 分支名称
        )


class TrunkVcsUrlListSchema(BaseSchema):
    data = base_fields.Nested(
        TrunkVcsUrlBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class UserBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'account',
            'realname',
            'role',
        )


class UserListSchema(BaseSchema):
    data = base_fields.Nested(
        UserBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class PlanBaseSchema(ModelSchema):
    members = base_fields.Nested(
        UserBaseSchema,
        many = True
    )
    projects = base_fields.Nested(
        ProjectBaseSchema,
        many = True
    )

    class Meta:
        model = ProjectPlanInfo
        fields = (
            'plan_id',  # 项目id
            'plan_code',  # 项目代号
            'plan_name',  # 项目名称
            'expect_integration_date',  # 预计集成日期
            'expect_release_date',  # 预计上线日期
            'plan_status',  # 状态
            'zt_id',  # 状态
            'start_date',
            'end_date',
            'members',
            'PM',  # 项目负责人account
            'PM_name',
            'create_date',
            'projects',
            'notCreateProCount',
            'isEmergency',
            'bug_num',
            'need_num',
            'test_head',
            'test_head_name',
            'error_desc',
            'link_product',
            'last_build_time',
        )


class PlanSchema(ModelSchema):
    # members = base_fields.Nested(
    #     UserBaseSchema,
    #     many = True
    # )

    class Meta:
        model = ProjectPlanInfo
        fields = (
            'plan_id',  # 项目id
            'plan_code',  # 项目代号
            'plan_name',  # 项目名称
            'expect_integration_date',  # 预计集成日期
            'expect_release_date',  # 预计上线日期
            'plan_status',  # 状态
            'zt_id',  # 状态
            'start_date',
            'end_date',
            'PM',  # 项目负责人account
            'PM_name',
            'create_date',
            'isEmergency',
            'bug_num',
            'need_num',
            'isDisable',  # 是否可撤下
        )


class PlanListSchema(BaseSchema):
    data = base_fields.Nested(
        PlanBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class PlanInfoSchema(BaseSchema):
    data = base_fields.Nested(
        PlanBaseSchema,
        many = False
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )

class CreatePlanByPrd(BaseSchema):
    data = base_fields.basestring

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ApplyInfoBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'type',  # dev/qa
            'agent_ip',
            'status',
        )


class ApplyInfoSchema(ModelSchema):
    data = base_fields.Nested(
        ApplyInfoBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class CheckProjectPlanCodeBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'name',
            'msg',
        )


class CheckProjectPlanCodeSchema(ModelSchema):
    data = base_fields.Nested(
        CheckProjectPlanCodeBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )
